package com.bxx.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bxx.common.lang.Result;
import com.bxx.entity.Tourist;
import com.bxx.service.TouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TouristController {

    @Autowired
    TouristService touristService;
    @PostMapping("/tourist/save")
    public Result save(@Validated @RequestBody Tourist tourist){
        Tourist tourist1 = touristService.getOne(new QueryWrapper<Tourist>().eq("email", tourist.getEmail()));
        if(tourist1 == null){
            touristService.save(tourist);
        }else{
            Tourist temp = new Tourist();
            temp.setName(tourist.getName());
            touristService.update(temp, new UpdateWrapper<Tourist>().eq("email", tourist.getEmail()));
        }
        Tourist tourist2 = touristService.getOne(new QueryWrapper<Tourist>().eq("email", tourist.getEmail()));
        return Result.succ(tourist2);

    }
}
